﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace BingoLib
{
    public class Collect
    {
        /// <summary>
        /// 获取网页源代码
        /// </summary>
        /// <param name="url">要访问的网址</param>
        /// <param name="ref_url">来源网址</param>
        /// <param name="encode">编码</param>
        /// <returns>bool</returns>
        public static bool getHtml(string url, string ref_url, Encoding encode,out string HtmlCode)
        {
            HtmlCode = "";
            bool b = false;
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse webResponse = null;
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url.ToLower().StartsWith("http://") ? url : ("http://" + url));
                httpWebRequest.Referer = ref_url;
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727,; .NET CLR 3.0.04506,; .NET CLR 3.5.21022)";
                httpWebRequest.Method = "GET";
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encode);
                    HtmlCode = sr.ReadToEnd();
                    b = true;
                    sr.Close();
                }
                webResponse.Close();
            }
            catch
            {
                return b;
            }

            return b;
        }
    }
}
